import java.util.*;

public class Hash {
    public static void main(String[] args) {
        StringBuilder s = new StringBuilder();
    }

    /**
     * 最长公共前缀
     */
    //统一比较
    public String longestCommonPrefix(String[] strs) {
        if(strs.length == 1) return strs[0];
        int i = 0;
        for( ;i < strs[0].length(); i++){
            char tmp = strs[0].charAt(i);
            for(int j = 1; j < strs.length; j++){
                if(i == strs[j].length() || strs[j].charAt(i) != tmp){
                    return strs[0].substring(0,i);
                }
            }
        }
        return strs[0];
    }
    //一个一个比较
    public String longestCommonPrefix2(String[] strs) {
        String pre = strs[0];
        for(int i = 1; i < strs.length; i++){
            String c2 = strs[i];
            if(!pre.isEmpty() && !c2.isEmpty() && pre.charAt(0) == c2.charAt(0)){
                int j = 0;
                while(j < pre.length() && j < c2.length() && pre.charAt(j)==c2.charAt(j)){
                    j++;
                }
                pre = pre.substring(0,j);
            }else{
                return "";
            }
        }
        return pre;
    }
    /**
     * 字母异位词分组
     */
    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String, List<String>> map = new HashMap<>();
        List<List<String>> ret = new LinkedList<>();
        for (String s : strs) {
            char[] c = s.toCharArray();
            Arrays.sort(c);
            String tmp = new String(c);
            if (map.containsKey(tmp)) {
                map.get(tmp).add(s);
            } else {
                List<String> l = new LinkedList<>();
                l.add(s);
                map.put(tmp, l);
                ret.add(l);
            }
        }
        return ret;
    }
}
